package cn.com.jslh.boarpro.util;

import android.content.Context;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.com.jslh.boarpro.base.BaseApplication;
import cn.com.jslh.boarpro.tables.StriplineTable;
import cn.com.jslh.boarpro.tables.ZhouHaoTable;
import cn.com.jslh.boarpro.tables.ZhuSheTable;
import cn.com.jslh.boarpro.util.database.DataBaseUtil;

/**
 * 作者：周洲 on 2017/7/27 14:15
 * <p>
 * 邮箱：double0zhou@126.com
 */

public class DownLoadBaseDataUtil {

    public static void downLoadAllData(Context context) {
        downLoadLineData(context, null);
//        downLoadZhouHaoData(context, null);
//        downLoadZhuSheData(context, null);
    }

    /**
     * 下载生产线信息
     *
     * @param context
     * @param netSuccessListener
     */
    public static void downLoadLineData(final Context context, final WebService.NetSuccessListener netSuccessListener) {
        new WebService.Builder(context, "exportLineData", "WSZpigBaseDataFacade")
                .addProperty("orgID", SPUtils.getInstance().getString(BaseApplication.FORGID, ""))
                .success(new WebService.NetSuccessListener() {
                    @Override
                    public void Success(String s) throws JSONException {
                        JSONObject jsonObject = new JSONObject(s);
                        String flag = jsonObject.getString("flag");
                        switch (flag) {
                            case "1"://执行成功
                                JSONArray jsonArray = jsonObject.getJSONArray("array");
                                DataBaseUtil.clearTable(context, StriplineTable.class);
                                DataBaseUtil.saveJsonArrayData(context, StriplineTable.class, jsonArray);
                                if (netSuccessListener != null)
                                    netSuccessListener.Success("");
                                break;
                            case "0":
                                Toast.makeText(context, jsonObject.getString("warn"), Toast.LENGTH_SHORT).show();
                                break;//执行失败
                        }
                    }
                }).post();
    }

    /**
     * 下载猪舍信息
     *
     * @param context
     * @param netSuccessListener
     */
    public static void downLoadZhuSheData(final Context context, final WebService.NetSuccessListener netSuccessListener) {

        new WebService.Builder(context, "exportZhuShe", "WSZpigBaseDataFacade")
                .addProperty("lineID", SPUtils.getInstance().getString(BaseApplication.FLINEID))
                .addProperty("orgID", SPUtils.getInstance().getString(BaseApplication.FORGID))
                .addProperty("bizType","orgAll")
                .success(new WebService.NetSuccessListener() {
                    @Override
                    public void Success(String s) throws JSONException {
                        JSONObject jsonObject = new JSONObject(s);
                        String flag = jsonObject.getString("flag");
                        switch (flag) {
                            case "1"://执行成功
                                JSONArray jsonArray = jsonObject.getJSONArray("array");
                                DataBaseUtil.clearTable(context, ZhuSheTable.class);
                                DataBaseUtil.saveJsonArrayData(context, ZhuSheTable.class, jsonArray);
                                if (netSuccessListener != null)
                                    netSuccessListener.Success("");
                                break;
                            case "0":
                                Toast.makeText(context, jsonObject.getString("warn"), Toast.LENGTH_SHORT).show();
                                break;//执行失败
                        }
                    }
                }).post();

    }

}
